import Vue from 'vue';

import App from './App';
import router from './router';
import store from './store'
import 'mint-ui/lib/style.css'
import './assets/less/index.less';
import * as filters from './filters';
import cookie from './assets/js/cookie';
import { Popup, Picker, Swipe, SwipeItem, IndexList, IndexSection, MessageBox, Toast, InfiniteScroll, Spinner, Indicator,Loadmore} from 'mint-ui';
import price from './components/price/price';
import priceShow from './components/price/priceShow';
import nodata from './components/noData/nodata.vue';
import pandaBi from './components/pandaBi/pandaBi.vue';
import loading from './components/loading/loading';
import headBar from './components/header/header';
import footBar from './components/foot/foot';
import * as Api from './services/api';

Vue.config.silent = true;
Vue.config.productionTip = false;
Vue.config.devtools = false;
Vue.config.errorHandler = function (e) {
  console.log(e);
};
Vue.component('price', price);
Vue.component('priceShow', priceShow);
Vue.component('nodata', nodata);
Vue.component('pandaBi', pandaBi);
Vue.component('xm-loading', loading);
Vue.component('headBar', headBar);
Vue.component('footBar', footBar);

Vue.component(Popup.name, Popup);
Vue.component(Picker.name, Picker);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(IndexList.name, IndexList);
Vue.component(IndexSection.name, IndexSection);
Vue.component(Spinner.name, Spinner);
Vue.component(Loadmore.name, Loadmore);
Vue.use(InfiniteScroll);
window.MessageBox = MessageBox;
window.Indicator = Indicator;
window.Toast = Toast;
window.Cookie = cookie;
window.Store = store;
// window.pathReg = /^(login)|(complete)|(check)|(checkFail)|(register)|(pwdSuc)|(forgetPwd)|(rePwd)$/;
window.pathReg = /^(login)|(complete)|(check)|(checkFail)|(register)|(pwdSuc)|(forgetPwd)|(rePwd)|(home)|(category)|(goodsList)|(search)|(goodsDetail)|(hot)|(newGoods)|(museum)|(newbie)|(build)$/;
router.beforeEach((to, from, next)=>{
  let status = cookie.get('auditStatus');
  if (to.meta.title) {
      document.title = to.meta.title;
  }
  if(pathReg.test(to.name)){
    next();
    return;
  }
  if(!status){
    location.href = '/#/login';
    return;
  }else if(status == 1){ //待完善资料
    location.href = '/#/complete';
    return;
  }else if(status == 2){//待审核
    location.href = '/#/check';
    return;
  }else if(status == 4){//审核未通过
    location.href = '/#/checkFail';
    return;
  }
  next();
});
// register global utility filters.
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
});
// register global api
Object.keys(Api).forEach(key => {
  window[key] = Api[key];
})


window.vm = new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
});
